﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using EntityManage;

public partial class Modules_Default_MyToDo : System.Web.UI.Page
{
    IPerf_FlowHandler flowHandler = new Perf_FlowManage();
    IPerf_Flow_StepsHandler fstepHandler = new Perf_Flow_StepsManage();
    ISys_UserHandler userHandler = new Sys_UserManage();
    IPerf_Flow_TraceHandler ftHandler = new Perf_Flow_TraceManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["OrderBy"] = "Id";
            ViewState["Accending"] = "asc";

            dataBind(string.Empty, null);
          
        }
    }

    /// <summary>
    /// 初始化岗位信息表
    /// </summary>
    private void dataBind(string whereClause, SqlParameter[] parms)
    {
        try
        {
            int userId = int.Parse(Session["CurrentUserID"].ToString());
            Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(userId);

            Perf_FlowQuery pfaQuery = flowHandler.GetPerf_FlowQuery((int)Enums.PerfFlowIDs.PersonalFirstAssignApproval);
            bool hasFirstStepID = false;
            string myStepIds = GetFlowTraceIDs(userQuery, pfaQuery, ref hasFirstStepID);

            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Columns.Add("Value");

            DataRow dr;

            // 个人绩效指标分配审批
            int value = 0;
            string whereSql = " Status = @Status and Flow_ID = @Flow_ID and ((Current_Step = 0 and " + (hasFirstStepID ? "1=1" : "1!=1") + ") or Current_Step_ID in (" + myStepIds.ToString() + "))";
            parms = new SqlParameter[] { new SqlParameter("@Status", (int)Enums.FlowTraceStatus.OnGoing), new SqlParameter("@Flow_ID", pfaQuery.ID) };
            value = ftHandler.GetCounter(whereSql, parms);
            if (value > 0)
            {
                dr = dt.NewRow();
                dr["ID"] = (int)Enums.PerfMyToDoListIDs.PersonalFirstAssignApproval;
                dr["Name"] = "个人绩效指标分配审批";
                dr["Value"] = value;

                dt.Rows.Add(dr);
            }

            // 个人绩效评分审批
            value = 0;
            pfaQuery = flowHandler.GetPerf_FlowQuery((int)Enums.PerfFlowIDs.PersonalEvaluateApproval);
            hasFirstStepID = false;
            myStepIds = GetFlowTraceIDs(userQuery, pfaQuery, ref hasFirstStepID);//把Current_Step＝0修改为Current_Step=1
            whereSql = " Status = @Status and Flow_ID = @Flow_ID and ((Current_Step = 1 and " + (hasFirstStepID ? "1=1" : "1!=1") + ") or Current_Step_ID in (" + myStepIds.ToString() + "))";
            parms = new SqlParameter[] { new SqlParameter("@Status", (int)Enums.FlowTraceStatus.OnGoing), new SqlParameter("@Flow_ID", pfaQuery.ID) };
            value = ftHandler.GetCounter(whereSql, parms);

            if (value > 0)
            {
                dr = dt.NewRow();
                dr["ID"] = (int)Enums.PerfMyToDoListIDs.PersonalEvaluateApproval;
                dr["Name"] = "个人绩效评分审批";
                dr["Value"] = value;

                dt.Rows.Add(dr);
            }

            // 个人绩效申诉审批
            value = 0;
            pfaQuery = flowHandler.GetPerf_FlowQuery((int)Enums.PerfFlowIDs.OtherPersonalPerfAppeal);
            hasFirstStepID = false;
            myStepIds = GetFlowTraceIDs(userQuery, pfaQuery, ref hasFirstStepID);
            whereSql = " Status = @Status and Flow_ID = @Flow_ID and ((Current_Step = 0 and " + (hasFirstStepID ? "1=1" : "1!=1") + ") or Current_Step_ID in (" + myStepIds.ToString() + "))";
            parms = new SqlParameter[] { new SqlParameter("@Status", (int)Enums.FlowTraceStatus.OnGoing), new SqlParameter("@Flow_ID", pfaQuery.ID) };
            value = ftHandler.GetCounter(whereSql, parms);

            if (value > 0)
            {
                dr = dt.NewRow();
                dr["ID"] = (int)Enums.PerfMyToDoListIDs.OtherPersonalPerfAppeal;
                dr["Name"] = "个人绩效申诉审批";
                dr["Value"] = value;

                dt.Rows.Add(dr);
            }

            // 部门待办

            // 角色待办

            gvMyToDoList.DataSource = dt;
            gvMyToDoList.DataBind();
        }
        catch
        {
            Jscript.AlertInAjax("連接錯誤!", UpdatePanel1);
            return;
        }
    }

    private string GetFlowTraceIDs(Sys_UserQuery userQuery, Perf_FlowQuery pfaQuery, ref bool hasFirstStepID)
    {
        string whereSql = BusinessManage.GetFlowStepsSql(pfaQuery.ID, 1, userQuery);
        SqlParameter[]  parms = new SqlParameter[] { };
        Perf_Flow_StepsQuery[] stepList = fstepHandler.GetPerf_Flow_StepsQueryList(whereSql, parms, " Step_Num asc");

        StringBuilder myStepIds = new StringBuilder();
        
        if (stepList != null && stepList.Length > 0)
        {
            foreach (Perf_Flow_StepsQuery stepQuery in stepList)
            {
                myStepIds.Append(stepQuery.ID);
                myStepIds.Append(",");
                if (stepQuery.Step_Num == 1 && stepQuery.Prev_ID == 0)
                    hasFirstStepID = true;
            }
            myStepIds = myStepIds.Remove(myStepIds.Length - 1, 1);
        }

        return myStepIds.ToString();
    }

    protected void gvMyToDoList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //
        if (e.CommandName == "view")
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString().Trim());
            try
            {
                if (id == (int)Enums.PerfMyToDoListIDs.PersonalFirstAssignApproval)
                    Response.Redirect("~/Modules/Personal/TargetAssignApproval.aspx");
                else if (id == (int)Enums.PerfMyToDoListIDs.PersonalEvaluateApproval)
                    Response.Redirect("~/Modules/Personal/PerfApplyApprove.aspx");
                else if (id == (int)Enums.PerfMyToDoListIDs.OtherPersonalPerfAppeal)
                    Response.Redirect("~/Modules/Default/AppealApprove.aspx");
            }
            catch(Exception es)
            {
                string m = es.Message;
                Jscript.AlertInAjax("操作失败!", UpdatePanel1);
            }
        }
    }

}
